package com.flyco.modules.attendance.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.common.api.vo.Result;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.modules.attendance.entity.UmsDayAttendance;
import com.flyco.modules.attendance.service.IumsDayAttendanceService;
import com.flyco.modules.attendance.vo.CheckinoutVO;
import com.flyco.modules.attendance.vo.DayAttendanceVO;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 员工日考勤表
 * @Author: flyco
 * @Date:   2022-11-02
 * @Version: V1.0
 */
@Slf4j
@Api(tags="员工日考勤表")
@RestController
@RequestMapping("/f/attendance/umsDayAttendance")
public class umsDayAttendanceController extends BaseController<UmsDayAttendance, IumsDayAttendanceService> {
	@Autowired
	private IumsDayAttendanceService umsDayAttendanceService;
	@Autowired
	private IStmStoreInfoService stmStoreInfoService;

	/**
	 * 分页列表查询
	 *
	 * @param umsDayAttendance
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "员工日考勤表-分页列表查询")
	@ApiOperation(value="员工日考勤表-分页列表查询", notes="员工日考勤表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(UmsDayAttendance umsDayAttendance,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<UmsDayAttendance> queryWrapper = QueryGenerator.initQueryWrapper(umsDayAttendance, req.getParameterMap());
		queryWrapper.orderByDesc("date");
		Page<UmsDayAttendance> page = new Page<UmsDayAttendance>(pageNo, pageSize);
		IPage<UmsDayAttendance> pageList = umsDayAttendanceService.page(page, queryWrapper);
		if(!pageList.getRecords().isEmpty()){
			List<UmsDayAttendance> records = pageList.getRecords();
			//查询店铺
			List<Long> storeIds = records.stream().map(UmsDayAttendance::getStoreId).distinct().collect(Collectors.toList());
			Map<Long, String> storeMap = stmStoreInfoService.list(Wrappers.lambdaQuery(StmStoreInfo.class).in(StmStoreInfo::getStoreId, storeIds)).stream().collect(Collectors.toMap(StmStoreInfo::getStoreId, StmStoreInfo::getName));

			//组装返回值
			records.forEach(one -> {
				String storeName = storeMap.get(one.getStoreId());
				if(StrUtil.isNotBlank(storeName)){
					one.setStoreName(storeName);
				}
			});
		}
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param umsDayAttendance
	 * @return
	 */
	@AutoLog(value = "员工日考勤表-添加")
	@ApiOperation(value="员工日考勤表-添加", notes="员工日考勤表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody UmsDayAttendance umsDayAttendance) {
		umsDayAttendanceService.save(umsDayAttendance);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param umsDayAttendance
	 * @return
	 */
	@AutoLog(value = "员工日考勤表-编辑")
	@ApiOperation(value="员工日考勤表-编辑", notes="员工日考勤表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody UmsDayAttendance umsDayAttendance) {
		umsDayAttendanceService.updateById(umsDayAttendance);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "员工日考勤表-通过id删除")
	@ApiOperation(value="员工日考勤表-通过id删除", notes="员工日考勤表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		umsDayAttendanceService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "员工日考勤表-批量删除")
	@ApiOperation(value="员工日考勤表-批量删除", notes="员工日考勤表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.umsDayAttendanceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "员工日考勤表-通过id查询")
	@ApiOperation(value="员工日考勤表-通过id查询", notes="员工日考勤表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		UmsDayAttendance umsDayAttendance = umsDayAttendanceService.getById(id);
		return Result.ok(umsDayAttendance);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param umsDayAttendance
   */
  @ApiOperation(value="导出excel", notes="导出excel")
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, UmsDayAttendance umsDayAttendance) {
      return super.exportXls(request, umsDayAttendance, UmsDayAttendance.class, "员工日考勤表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, UmsDayAttendance.class);
  }

}
